<%@page import="java.io.FileInputStream"%>
<%@page import="java.io.File"%>
<%@page import="java.io.BufferedOutputStream"%>
<%@page import="java.io.BufferedInputStream"%>
<%@ page language="java"   contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="/includes.jsp"%>
<%
	String strErrFile = request.getParameter("errFile");
	System.out.println("Error File" + strErrFile);
	if ( !fileDownload( response, strErrFile) ) {
%>
	<script>alert("파일 다운로드중에 에러가 발생하였습니다.");</script>
<%}%>

<%! 
	public boolean fileDownload(HttpServletResponse response, String errFile) {
		boolean isSuccess = false; // 만약 성공이된다면, respose가 close가 될것이다.
		
		BufferedInputStream bis = null;
		BufferedOutputStream bos = null;
		
		String fileName = errFile.substring(errFile.lastIndexOf("/") + 1);
		System.out.println("File Download Path = " + errFile);
		System.out.println("File Download fileName = " + fileName);
		try {
			File file = new File( errFile );
			if ( file != null && file.isFile() ) {
				byte b[] = new byte[1024];
				response.reset();
				response.resetBuffer();
				response.setContentLength((int)file.length());
		
				response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ";");
				bos = new BufferedOutputStream(response.getOutputStream());
				bis = new BufferedInputStream(new FileInputStream(file));
				int read = 0;
		
				while ((read = bis.read(b , 0 , 1024) ) != -1) {
					bos.write(b,0,read);
				}
				bos.flush();
				isSuccess = true;
			} 
		} catch ( Exception e ) {
			System.out.println("e ["+e+"]");
			e.printStackTrace();
		} finally {
			if ( bos != null ) try { bos.close(); } catch ( Exception e1 ) {}
			if ( bis != null ) try { bis.close(); } catch ( Exception e1 ) {}
		}
		return isSuccess;
	}
%>